package com.test.sshshiro.framework.intercepter;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.test.sshshiro.framework.annotation.AuthRequired;
import com.test.sshshiro.util.AppConstant;

public class CheckLoginIntercepter implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		AuthRequired authRequiredMethod = null;
		if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
			authRequiredMethod = ((HandlerMethod) handler).getMethodAnnotation(AuthRequired.class);
		}
		if ((authRequiredMethod == null || authRequiredMethod.validate() == false)) {
			return true;
		}
		String contextPath = request.getContextPath();
		String url = request.getServletPath().toString();
		HttpSession session = request.getSession();
		/*
		String user = (String) session.getAttribute(AppConstant.SESSION_USER_ID);
		if (StringUtils.isNoneEmpty(user)) {
			response.sendRedirect(contextPath + "/login?redirectURL=" + URLEncoder.encode(url));
			return false;
		}
		*/
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
